home *** CD-ROM | disk | FTP | other *** search
/ Amiga Tools 5 / Amiga Tools 5.iso / tools / disk-tools / hdoff / getstats.c < prev    next >
C/C++ Source or Header  |  1996-03-11  |  4KB  |  118 lines

  1. ; /* Execute me to compile! 
  2. sc GetStats.c link nomath cpu=any ignore=73
  3. quit
  4. */
  5.  
  6. /*GetStats - a demonstration on how to use HDOff's messageport
  7.   © by Gideon Zenz, 1996 - freely distributable
  8.   debugged and improved by Matthias Andree, 1996
  9.   This was written to give the user a tool to have a look at HDOff's
  10.   actual status and to have at least one example how to use the
  11.   messyport :)
  12.  
  13.   You may use this to build up your own programms.
  14.   This program was compiled using the great SAS/C v6.56 © by SAS Institute Inc.
  15.  
  16.   History:
  17.   1.00 initial by G. Zenz
  18.   1.01 improved by M. Andree
  19.        corrected typos, implemented second display for remaining time
  20.        float math no more needed, thus reducing executable size by 4k.
  21. */
  22.  
  23. /*** include all stuff */
  24. #include <stdio.h>
  25. #include <stdlib.h>
  26. #include <exec/exec.h>
  27. #include <proto/exec.h>
  28. #include <dos/dos.h>
  29. #include <proto/dos.h>
  30. #include <clib/alib_protos.h>
  31.  
  32. #pragma msg 73 warn
  33.  
  34. /* define HDOff's commands*/
  35. #define hd_GetStats     0
  36. #define hd_SetStats     1
  37. #define hd_Subscribe    2
  38. #define hd_Unsubscribe  3
  39. #define hd_StopDrive    4
  40. #define hd_Quit         5
  41. #define hd_ForceQuit    6
  42. #define hd_Die          -1
  43.  
  44. /* The message-structure*/
  45.  struct HD {
  46.      struct  Message HD_Msg;
  47.      WORD    HD_Cmd;
  48.      UWORD   HD_TimeHD0;
  49.      UWORD   HD_TimeHD1;
  50.      UWORD   HD_TimeLeftHD0;
  51.      UWORD   HD_TimeLeftHD1;
  52.      BOOL    HD_StatHD0;
  53.      BOOL    HD_StatHD1;
  54.      ULONG   HD_PortVer;
  55.      LONG    HD_Reserved;
  56.      };
  57.  
  58. /*the obligatory version string :)*/
  59. const UBYTE *version = "$VER: GetStats 1.01 (09.03.96)";
  60.  
  61. main() {
  62.     struct MsgPort *MyPort;
  63.     struct MsgPort *HDPort;
  64.     struct HD      *MyMsg;
  65.  
  66.     /* Try to create a messageport without name and the priority -1*/
  67.     if( !(MyPort=CreatePort(NULL, -1))) {
  68.         printf("Couldn't create messageport!\n");
  69.         exit(RETURN_FAIL);
  70.         }
  71.  
  72.     /*Now we allocate the memory used for our messages*/
  73.     if (MyMsg = (struct HD *) AllocMem(sizeof(struct HD), MEMF_PUBLIC | MEMF_CLEAR)) 
  74.     {
  75.         MyMsg->HD_Msg.mn_ReplyPort=MyPort;         /*Where HDOff should reply to...*/
  76.         MyMsg->HD_Msg.mn_Node.ln_Type=NT_MESSAGE;
  77.         MyMsg->HD_Msg.mn_Length=sizeof(struct HD);
  78.         MyMsg->HD_Reserved=0L;                     /*To be sure!*/
  79.         MyMsg->HD_Cmd=hd_GetStats;                 /*Our command*/
  80.  
  81.         /*NOTE: Forbid'ing is essential! HDOff could go away right after we
  82.           got its port, and we would post a message to an invalid address!*/
  83.         Forbid();
  84.         if((HDPort=FindPort("HDOFF_PORT"))) {    /*Find HDOff's port address*/
  85.             PutMsg(HDPort, (struct Message  *) MyMsg);
  86.             Permit();
  87.             }
  88.         else {              /*We couldn't find HDOff!*/
  89.             Permit();
  90.             printf("HDOff isn't active!\n");
  91.             FreeMem(MyMsg, sizeof(struct HD));
  92.             DeletePort(MyPort);
  93.             exit(RETURN_FAIL);
  94.             }
  95.  
  96.         WaitPort(MyPort);      /*Wait for an answer...*/
  97.  
  98.         MyMsg = (struct HD *)GetMsg(MyPort);        /*Get the answer from the waiting stack*/
  99.  
  100.         /*Print every information we got:*/
  101.         printf("Current status of HDOff:\nStart time HD0: %d min\nStart time HD1: %d min\nTime left HD0: %d:%02d min\nTime left HD1: %d:%02d min\nStatus HD0: %s\nStatus HD1: %s\nPort version: %d.%02d\n",
  102.                 (MyMsg->HD_TimeHD0/60), (MyMsg->HD_TimeHD1/60),
  103.                 (MyMsg->HD_TimeLeftHD0/60), (MyMsg->HD_TimeLeftHD0%60),
  104.                 (MyMsg->HD_TimeLeftHD1/60), (MyMsg->HD_TimeLeftHD1%60),
  105.                      ((MyMsg->HD_StatHD0)?"OFF":"ON"), ((MyMsg->HD_StatHD1)?"OFF":"ON"), 
  106.                 ((MyMsg->HD_PortVer)/100), ((MyMsg->HD_PortVer)%100));
  107.  
  108.         FreeMem(MyMsg, sizeof(struct HD));
  109.         } else
  110.             printf("Couldn't allocate message!\n");
  111.  
  112.     Forbid();
  113.     while(MyMsg = (struct HD *)GetMsg(MyPort));
  114.  
  115.     DeletePort(MyPort);
  116.     Permit();
  117.     }
  118.